//
//  TSTApi.m
//  TST
//
//  Created by POP3mac on 2020/4/28.
//  Copyright © 2020 YTYanK. All rights reserved.
//

#import "TSTApi.h"

@implementation TSTApi
yty_for_implementation(TSTApi)

// perasonalModel
- (UserModel *)userModel {
    if (_userModel == nil) {
        YYCache * _pCache = [YYCache cacheWithName:AULogin];
        if([_pCache containsObjectForKey:@"UserModel"]) {
          id model = [_pCache objectForKey:@"UserModel"];
          return _userModel =[UserModel yy_modelWithDictionary:model];
        }
    }
    return _userModel;
}


+ (void)requestWithUrl:(AUString)api parameters:(NSDictionary *)dic method:(NetMethod)met returnModel:(void (^)(id _Nonnull))block {
    [MBProgressHUD showMessage:@"正在驗證數據中..."];
    [YTYRequest requestWithUrl:api requestWithParameters:dic method:met returnSuccess:^(id  _Nonnull objs, int status, NSString * _Nonnull mag) {
        NSLog(@"---->>%@", objs);
        [MBProgressHUD hideHUD];
        if(status == 1) { // 成功 ，但是没有数据
            block(objs[@"data"]);
        }else if (status == 2) {
            block(objs[@"data"]);
        }else {
           if( mag.length > 0){
               [MBProgressHUD showError:mag];
           }
        }
        
    } returnError:^(NSString * _Nonnull err) {
        NSLog(@"---->>%@", err);
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:err];
    }];
}

+ (void)requestWithUrl:(AUString)api parameters:(NSDictionary *)dic method:(NetMethod)met returnModel:(void (^)(id _Nonnull))block returnError:(void (^)(id _Nonnull))errBlock {
    [MBProgressHUD showMessage:@"正在驗證數據中..."];
    [YTYRequest requestWithUrl:api requestWithParameters:dic method:met returnSuccess:^(id  _Nonnull objs, int status, NSString * _Nonnull mag) {
        NSLog(@"obj---->>%@", objs);
//        [YSBNetRequest hideLoading];
        [MBProgressHUD hideHUD];
        if(status == 1) {
            if ([[objs[@"code"] stringValue] isEqualToString:@"-1"]) {
                  [MBProgressHUD showError:objs[@"msg"]];
            }else {
                  block(objs[@"data"]);
            }
           
        }else if (status == 2) { // 成功 ，但是没有数据
             if ([[objs[@"code"] stringValue] isEqualToString:@"-1"]) {
                   [MBProgressHUD showError:objs[@"msg"]];
                   block(objs[@"data"]);
               }else {
                     block(objs[@"data"]);
               }
        }else {
           if( mag.length > 0){
               [MBProgressHUD showError:mag];
           }
        }
           
        
    } returnError:^(NSString * _Nonnull err) {
        NSLog(@"---->>%@", err);
        [MBProgressHUD hideHUD];
        errBlock(err);
        
    }];
    
}

@end
